package com.itkeien.streamprogramproject;

import java.util.Optional;

public class TestOptional {

    public static void main(String[] args) {
        System.out.println("Hello World!");

        System.out.println(Optional.ofNullable(null).orElse("null"));


        // Optional 构造方法 非空
        Optional<String> str = Optional.of("001");

        //ifPresent方法 存在则打印
        str.ifPresent(System.out::println);
        System.out.println(str.get());
        System.out.println(str.orElse("null"));

        //创建一个Optional对象 可以存储null
        Optional<String> str1 = Optional.ofNullable(null);
        System.out.println(str1.orElse("null"));
        System.out.println(str1.orElseGet(() -> "null"));

        Optional<String> s1 = str.filter(s -> s.length() == 0);
        System.out.println(s1.orElse("true"));

        Optional<String> s2 = str.map(s -> s + "1");
        System.out.println(s2.orElse("null"));

        Optional<String> s3 = str1.map(s -> s + "1");
        System.out.println(s3.orElse("null"));


    }
}
